#!/usr/bin/python3
# -*- coding=utf-8 -*-

from telnetlib import Telnet
import re
import time

def copyasa(username, password, rackno, verno, asano, ctxname = 'sys'):
    if rackno == 'BJRack01':
		rackip = '172.17.100.111'
	elif rackno == 'BJRack02':
		rackip = '172.17.100.112'
	elif rackno == 'BJRack03':
		rackip = '172.17.100.113'

	portno = 2006 + int(asano)

	tn = Telnet(rackip, portno)

#正则表达式匹配各种状态

#	yesorno = re.compile(r'.*Please answer.*')
	exc = re.compile(r'.*>.*')
	configure = re.compile(r'.*config.*')
	priv = re.compile(r'.*#.*')
	clearconfig = re.compile(r'ciscoasa(\(config\))?#')
	asaver = re.compile(r'^ASA Version .*')

#多次回车确认状态

	tn.write(b'\r\n')
	tn.read_very_eager()
	time.sleep(1)
	tn.write(b'\r\n')
	tn.read_very_eager()
	time.sleep(1)
	tn.write(b'\r\n')
	tn.read_very_eager()
	time.sleep(1)
	tn.write(b'\n')
	tn.write(b'\n')
	tn.write(b'\32\n')
	time.sleep(1)
	tn.write(b'\n')
	tn.write(b'\n')
	tn.write(b'\32\n')

#获取位置状态信息

	rackreply = tn.expect([],timeout=1)[2].decode().strip()

#基于状态采取行为
	while True:
		if configure.match(rackreply):
			#全局配置模式
			tn.write(b'\n')	
			time.sleep(2)
#			print('match configure')
			break

		elif exc.match(rackreply):
			#执行模式
			tn.write(b'\n')	
			tn.write(b'en\n')
			time.sleep(1)
			tn.write(b'\n')	
			tn.write(b'configure terminal\n')
			tn.write(b'n\n')				
#			print('match exec')
			break

		elif priv.match(rackreply):
			#特权模式
			tn.write(b'\n')	
			time.sleep(2)
			tn.write(b'\n')	
			tn.write(b'configure terminal\n')
			tn.write(b'n\n')
#			print('match priv')
			break
		else: 
			tn.write(b'\r\n')
			time.sleep(1)
			tn.write(b'\r\n')
			tn.read_very_eager()
#			print('next')
			rackreply = tn.expect([],timeout=1)[2].decode().strip()

#继续回车确认状态
	tn.write(b'\n\n\n')
	tn.read_very_eager()

#获取状态信息
	rackreply = tn.expect([],timeout=1)[2].decode().strip()
		
	tn.write(b'configure terminal\n\n')
	time.sleep(1)
	tn.write(b'configure terminal\n\n')
	time.sleep(1)
	tn.write(b'terminal pager 0\n\n')
	time.sleep(1)
	tn.write(b'terminal pager 0\n\n')
	time.sleep(1)
	tn.write(b'changeto system\n\n\n')	
	time.sleep(1)
	tn.write(b'wr mem all\n')
	time.sleep(2)
	rackreply = tn.expect([],timeout=2)[2].decode().strip()
	if ctxname == 'sys':
		tn.write(b'\n')
		time.sleep(2)
		tn.write(b'more system:running-config\n')
		rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
		configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + 'tmp' + '.txt'
		configtmp = open(configfiletmp, 'w')
		configtmp.write(rackreplyasat)
		configtmp.close()

		configtmpr = open(configfiletmp, 'r')

		rackreplylines = configtmpr.readlines()

		location = 1
		i = 1
		for line in rackreplylines:
			i = i + 1
			if asaver.match(line.strip()):
				location = i
		asaconfiglst = rackreplylines[location:]

		asaconfig = ''.join(str(e) for e in asaconfiglst)
		configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + '.txt'
		config = open(configfile, 'w')
		
		config.write(asaconfig)
		config.close()
		tn.close()
	else:
		tn.write(b'\n')
		time.sleep(2)
		tn.write(b'more system:running-config\n')
		rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
		configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + 'tmp' + '.txt'
		configtmp = open(configfiletmp, 'w')
		configtmp.write(rackreplyasat)
		configtmp.close()

		configtmpr = open(configfiletmp, 'r')

		rackreplylines = configtmpr.readlines()

		location = 1
		i = 1
		for line in rackreplylines:
			i = i + 1
			if asaver.match(line.strip()):
				location = i
		asaconfiglst = rackreplylines[location:]

		asaconfig = ''.join(str(e) for e in asaconfiglst)
		configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + '.txt'
		config = open(configfile, 'w')
		
		config.write(asaconfig)
		
		for ctx in ctxname:
			tn.write(b'\n')
			time.sleep(2)
			cmd = ('more flash:/%s.cfg\n' % ctx).encode()
#			print(cmd)
			tn.write(cmd)
			rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
#			print(rackreplyasat)
			configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + ctx + 'tmp' + '.txt'
			configtmp = open(configfiletmp, 'w')
			configtmp.write(rackreplyasat)
			configtmp.close()

			configtmpr = open(configfiletmp, 'r')

			rackreplylines = configtmpr.readlines()

			location = 1
			i = 1
			for line in rackreplylines:
				i = i + 1
				if asaver.match(line.strip()):
					location = i
			asaconfiglst = rackreplylines[location:]

			asaconfig = ''.join(str(e) for e in asaconfiglst)

			configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + ctx + '.txt'
			config = open(configfile, 'w')
			config.write(asaconfig)
			config.close()
	tn.close()


if __name__ == "__main__":
	copyasa('labtest', 'Cisc0123', 'BJRack03', 'v12', '1', ['Admin', 'C1', 'C2'])